package com.wit.locker.handle;

import com.wit.locker.domain.Result;
import com.wit.locker.exception.LockerException;
import com.wit.locker.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionHandle {

    private final Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        if (e instanceof LockerException) {
            LockerException lockerException = (LockerException) e;
            return ResultUtil.error(lockerException.getCode(), lockerException.getMessage());
        }
        logger.error("系统异常 {}", e);
        return ResultUtil.error(100, "未知错误");
    }

}

